using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text;
using System;

public class HallUI : MonoBehaviour
{    

    public GameObject MyRoomDiv;    
    public GameObject GoCreateDiv;
    public GameObject StateDiv;
    public List<Text> StatePlayerIDSList;
    public Text StateMyRoomID;
    public GameObject RoomListHandle;
    public GameObject RoomDivPrefab;

    public GameObject CreateRoomDiv;
    public InputField RoomIDInput;

    public bool openMyRoomDiv = false;
    public bool openCreateRoomDiv = false;

    private float fetchInterval = 3.0f;
    private float fetchTimer = 3.0f;

    // Start is called before the first frame update
    void Start()
    {
        RenderUI();
    }    

    // Update is called once per frame
    void Update()
    {
        fetchTimer += Time.deltaTime;
        if(fetchTimer > fetchInterval)
        {
            fetchTimer = 0;
            StartCoroutine(FetchRooms());
        }
    }

    public void CloseMyRoomDiv() {
        openMyRoomDiv = false;
        RenderUI();
    }

    public void OpenMyRoomDiv()
    {
        openMyRoomDiv = true;
        RenderUI();
    }

    public void OpenCreateRoomDiv()
    {
        openCreateRoomDiv = true;
        RenderUI();
    }

    public void JoinMyRoom()
    {
        Game.Instance.Logic.Data.JoinedRoomID = Game.Instance.Logic.Data.MyRoomID;
        Network.Instance.Connect("127.0.0.1", 10086);
        var b = pb.C2S_ConnectMsg.CreateBuilder();

        b.SetBattleID((UInt64) Game.Instance.Logic.Data.JoinedRoomID);        
        b.SetPlayerID(Game.Instance.Logic.Data.MyID);
        b.SetToken("");
        Network.Instance.client.Send(PacketWraper.NewPacket(pb.ID.MSG_Connect, b.Build()));
    }

    public void CloseCreateRoomDiv()
    {
        openCreateRoomDiv = false;
        RenderUI();
    }

    public void CreateRoom()
    {
        StartCoroutine(DoCreateRoom());
    }

    IEnumerator DoCreateRoom()
    {
        WWW getData = new WWW("http://127.0.0.1/create?room=" + RoomIDInput.text + "&owner=" + Game.Instance.Logic.Data.MyID);
        Game.Instance.Logic.Data.MyRoomID = int.Parse(RoomIDInput.text);
        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            openMyRoomDiv = true;
            openCreateRoomDiv = false;
            RenderUI();
        }
    }

    IEnumerator FetchRooms()
    {
        WWW getData = new WWW("http://127.0.0.1/get");
        yield return getData;
        if (getData.error != null)
        {
            Debug.Log(getData.error);
        }
        else
        {
            Debug.Log(getData.text);
            GameData.RoomListWrapper roomListWrapper = JsonUtility.FromJson<GameData.RoomListWrapper>(getData.text);
            Game.Instance.Logic.Data.Rooms = roomListWrapper.Rooms;
            foreach (var item in Game.Instance.Logic.Data.Rooms)
            {
                if ((int)item.RoomID == Game.Instance.Logic.Data.MyRoomID || item.OwnerID == Game.Instance.Logic.Data.MyID)
                {
                    Game.Instance.Logic.Data.MyRoom = item;
                    Game.Instance.Logic.Data.MyRoomID = (int) item.RoomID;
                }
            }
            RenderUI();
        }
    }



    // UI MVC
    public void RenderUI()
    {
        RenderMyRoomDiv();
        RenderCreateRoomDiv();
        RenderRoomsDiv();
    }

    public void RenderMyRoomDiv() {
        MyRoomDiv.active = openMyRoomDiv;
        RenderGoCreateDiv();
        RenderStateDiv();
    }
    public void RenderStateDiv() {        
        StateDiv.active = (Game.Instance.Logic.Data.MyRoomID != -1);
        if (StateDiv.active)
        {
            for (int i = 0; i < 2; ++i)
            {
                StatePlayerIDSList[i].text = "~";
            }
            if (Game.Instance.Logic.Data.MyRoom != null && Game.Instance.Logic.Data.MyRoom.Players != null)
            {
                for (int i = 0; i < Game.Instance.Logic.Data.MyRoom.Players.Count; ++i)
                {
                    StatePlayerIDSList[i].text = Game.Instance.Logic.Data.MyRoom.Players[i];
                }
            }
            StateMyRoomID.text = Game.Instance.Logic.Data.MyRoomID.ToString();
        }
    }
    public void RenderGoCreateDiv() {
        GoCreateDiv.active = (Game.Instance.Logic.Data.MyRoomID == -1);
    }


    public void RenderCreateRoomDiv()
    {
        CreateRoomDiv.active = openCreateRoomDiv;
    }

    public void RenderRoomsDiv() {
        RoomListHandle.GetComponent<RoomListHandle>().clear();
        if (Game.Instance.Logic.Data.Rooms == null)
            return;
        int idx = 0;
        foreach (var item in Game.Instance.Logic.Data.Rooms)
        {
            GameObject go = GameObject.Instantiate(RoomDivPrefab);
            go.transform.parent = RoomListHandle.transform;
            go.GetComponent<RectTransform>().position = new Vector3(200 + idx * 300, 200, 100);
            go.GetComponent<RoomItemUI>().RoomIdText.text = item.RoomID.ToString();
            go.GetComponent<RoomItemUI>().CountText.text = item.Players.Count.ToString() + "/2";
            if (item.Players.Count == 1)
            {
                go.GetComponent<RoomItemUI>().Player1Text.text = item.Players[0];
                go.GetComponent<RoomItemUI>().Player2Text.text = "~";
            }
            else if (item.Players.Count == 2)
            {
                go.GetComponent<RoomItemUI>().Player1Text.text = item.Players[0];
                go.GetComponent<RoomItemUI>().Player2Text.text = item.Players[1];
            }
            else {
                go.GetComponent<RoomItemUI>().Player1Text.text = "~";
                go.GetComponent<RoomItemUI>().Player2Text.text = "~";
            }
            idx++;
        }
    }
}
